package com.example.sunmiv1.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.ChildEntity;

import java.util.ArrayList;


/**
 *
 * @author Apathy、恆
 *
 * <br/>
 * <br/>
 *
 *         子類分組的適配器
 *
 * 三級分類的adpater
 *
 *
 *         方法{@link #isChildSelectable(int,int)} <b><font color='#ff00ff'
 *         size='2'>必須返回true</font></b>
 *
 * */
public class ChildAdapter extends BaseExpandableListAdapter {

    private Context mContext;// 上下文

    private int index;

    private ArrayList<ChildEntity> mChilds;// 數據源

    public ChildAdapter(Context context, ArrayList<ChildEntity> childs) {
        this.mContext = context;
        this.mChilds = childs;
    }

    public void isSelect(int index){
        this.index = index;
        notifyDataSetChanged();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mChilds.get(groupPosition).getChildNames() != null ? mChilds
                .get(groupPosition).getChildNames().size() : 0;
    }

    @Override
    public String getChild(int groupPosition, int childPosition) {
        if (mChilds.get(groupPosition).getChildNames() != null
                && mChilds.get(groupPosition).getChildNames().size() > 0)
            return mChilds.get(groupPosition).getChildNames()
                    .get(childPosition).toString();
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isExpanded, View convertView, ViewGroup parent) {
        ChildHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.child_child_item, null);
            holder = new ChildHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ChildHolder) convertView.getTag();
        }
        holder.update(getChild(groupPosition, childPosition));
        /*if(index==childPosition){
            holder.childChildTV.setTextColor(Color.parseColor("#FF6673"));
        }else{
            holder.childChildTV.setTextColor(Color.parseColor("#333333"));
        }*/
        return convertView;
    }

    /**
     * @author Apathy、恆
     *
     *         Holder優化
     * */
    class ChildHolder {

        private TextView childChildTV;

        public ChildHolder(View v) {
            childChildTV = (TextView) v.findViewById(R.id.childChildTV);
        }

        public void update(String str) {
            childChildTV.setText(str);
//            childChildTV.setTextColor(Color.parseColor("#333333"));
        }
    }

    @Override
    public Object getGroup(int groupPosition) {
        if (mChilds != null && mChilds.size() > 0)
            return mChilds.get(groupPosition);
        return null;
    }

    @Override
    public int getGroupCount() {
        return mChilds != null ? mChilds.size() : 0;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        GroupHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.child_group_item, null);
            holder = new GroupHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }
        holder.update(mChilds.get(groupPosition));
        return convertView;
    }

    /**
     * @author Apathy、恆
     *
     *         Holder優化
     * */
    class GroupHolder {

        private TextView childGroupTV;

        public GroupHolder(View v) {
            childGroupTV = (TextView) v.findViewById(R.id.childGroupTV);
        }

        public void update(ChildEntity model) {
            childGroupTV.setText(model.getGroupName());
            childGroupTV.setTextColor(model.getGroupColor());
        }
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        /**
         * ==============================================
         * 此處必須返回true，否則無法響應子項的點擊事件===============
         * ==============================================
         **/
        return true;
    }
}

